﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class SchoolProductionForm : Form
    {
        int regID, avpID, fhID, pfshID = -1, cultID, prkID, productID;
        tb_schoolproduct schoolProduct;
        const string catMeasure = "Dimension";

        public SchoolProductionForm()
        {
            InitializeComponent();
        }

        private void SchoolProductionForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            cbCulture.SelectedIndexChanged -= cbCulture_SelectedIndexChanged;
            cbCulture.DataSource = UtilClass.GetCBCultureDataSourse();
            cbCulture.DisplayMember = "Name";
            cbCulture.ValueMember = "ID";
            cbCulture.SelectedIndex = -1;
            cbCulture.SelectedIndexChanged += new EventHandler(cbCulture_SelectedIndexChanged);

            cbMeasure.DataSource = UtilClass.FillCBCategory(catMeasure);
            cbMeasure.ValueMember = "ID";
            cbMeasure.DisplayMember = "Name";
            cbMeasure.SelectedIndex = -1;
            cbMeasure.Text = string.Empty;
        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbFH.DataSource = null;
                cbPFSH.DataSource = null;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
                pfshID = 0;
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                cbFH.Enabled = true;
                cbFH.DataSource = DBClass.Farms
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbFH.DisplayMember = "Name";
                cbFH.ValueMember = "ID";
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbPFSH.DataSource = null;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
                pfshID = 0;
            }
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFH.SelectedIndex >= 0)
            {
                fhID = Convert.ToInt32(cbFH.SelectedValue);
                cbPFSH.Enabled = true;
                cbPFSH.SelectedIndexChanged -= cbPFSH_SelectedIndexChanged;
                cbPFSH.DataSource = DBClass.Schools
                    .Where(wh => wh.FarmID == fhID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbPFSH.DisplayMember = "Name";
                cbPFSH.ValueMember = "ID";
                cbPFSH.SelectedIndex = -1;
                cbPFSH.Text = string.Empty;
                cbPFSH.SelectedIndexChanged += new EventHandler(cbPFSH_SelectedIndexChanged);
                pfshID = 0;
            }
        }

        private void cbPFSH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPFSH.SelectedIndex >= 0)
            {
                pfshID = Convert.ToInt32(cbPFSH.SelectedValue);
                groupBox1.Enabled = true;
                if (productID > 0 && pfshID > 0)
                    ShowProduction(productID, pfshID, dtpDate.Value.Date);
            }
            else
            {
                pfshID = 0;
            }
        }

        private void cbCulture_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCulture.SelectedIndex >= 0)
            {
                cbProductKind.SelectedIndexChanged -= cbProductKind_SelectedIndexChanged;

                cultID = Convert.ToInt32(cbCulture.SelectedValue);
                cbProductKind.Enabled = true;

                if (InformationClass.Lang)
                    cbProductKind.DataSource = DBClass.ProductKids
                        .Where(wh => wh.CultureID == cultID)
                        .Select(se => new
                        {
                            Name = se.Name,
                            ID = se.ID
                        }).ToArray();
                else
                    cbProductKind.DataSource = DBClass.ProductKids
                        .Where(wh => wh.CultureID == cultID)
                        .Select(se => new
                        {
                            Name = se.NameUz,
                            ID = se.ID
                        }).ToArray();
                cbProductKind.DisplayMember = "Name";
                cbProductKind.ValueMember = "ID";
                cbProductKind.SelectedIndex = -1;
                cbProductKind.Text = string.Empty;

                cbProductKind.SelectedIndexChanged += new EventHandler(cbProductKind_SelectedIndexChanged);
                productID = 0;
            }
        }

        private void cbProductKind_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbProductKind.SelectedIndex >= 0)
            {
                prkID = Convert.ToInt32(cbProductKind.SelectedValue);

                cbProducts.Enabled = true;
                cbProducts.SelectedIndexChanged -= cbProducts_SelectedIndexChanged;

                if (InformationClass.Lang)
                    cbProducts.DataSource = DBClass.Products
                        .Where(wh => wh.ProductKindID == prkID)
                        .Select(se => new
                        {
                            ID = se.ID,
                            Name = se.Name
                        }).ToArray();
                else
                    cbProducts.DataSource = DBClass.Products
                        .Where(wh => wh.ProductKindID == prkID)
                        .Select(se => new
                        {
                            ID = se.ID,
                            Name = se.NameUz
                        }).ToArray();

                cbProducts.DisplayMember = "Name";
                cbProducts.ValueMember = "ID";
                cbProducts.SelectedIndex = -1;
                cbProducts.Text = string.Empty;
                productID = 0;
                cbProducts.SelectedIndexChanged += new EventHandler(cbProducts_SelectedIndexChanged);
            }
        }

        private void cbProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbProducts.SelectedIndex >= 0)
            {
                productID = Convert.ToInt32(cbProducts.SelectedValue);
                if (productID > 0 && pfshID > 0)
                    ShowProduction(productID, pfshID, dtpDate.Value.Date);
            }
            else
                productID = 0;
        }

        private void ShowProduction(int productID, int pfshID, DateTime dateTime)
        {
            schoolProduct = DBClass.SchoolProducts
                .Where(wh => wh.Date == dateTime && wh.ProductID == productID && wh.SchoolID == pfshID)
                .SingleOrDefault();

            if (schoolProduct != null)
            {
                tbArea.Text = schoolProduct.Area.Value.ToString();
                tbCost.Text = schoolProduct.Cost.Value.ToString();
                tbHarvestQuantity.Text = schoolProduct.HarvestQuantity.Value.ToString();
                cbMeasure.SelectedIndex = cbMeasure.FindStringExact(UtilClass.GetCategoryDisplayName((int)schoolProduct.Measure, catMeasure));
            }
            else
            {
                cbMeasure.SelectedIndex = -1;
                tbArea.Text = tbCost.Text = tbHarvestQuantity.Text = cbMeasure.Text = string.Empty;
            }
        }

        private void dtpDate_ValueChanged(object sender, EventArgs e)
        {
            if (productID > 0 && pfshID > 0)
                ShowProduction(productID, pfshID, dtpDate.Value.Date);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (pfshID > 0 && productID > 0)
            {
                decimal area, HQuantity, cost;
                int mess = cbMeasure.SelectedIndex >= 0 ? Convert.ToInt32(cbMeasure.SelectedValue) : 0;
                bool correct = tbArea.Text.GetDecimalOrDefault(out area);
                correct &= tbCost.Text.GetDecimalOrDefault(out cost);
                correct &= tbHarvestQuantity.Text.GetDecimalOrDefault(out HQuantity);

                if (!correct)
                    if (MessageBox.Show("Не верно заполненны значения. Продолжить?", "Внимание!", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                if (schoolProduct != null)
                {
                    schoolProduct.Area = area;
                    schoolProduct.Cost = cost;
                    schoolProduct.HarvestQuantity = HQuantity;
                    schoolProduct.Measure = mess;
                }
                else
                {
                    schoolProduct = new tb_schoolproduct
                    {
                        Area = area,
                        Cost = cost,
                        Date = dtpDate.Value.Date,
                        HarvestQuantity = HQuantity,
                        Measure = mess,
                        ProductID = productID,
                        SchoolID = pfshID
                    };
                    DBClass.SchoolProducts.InsertOnSubmit(schoolProduct);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
        }

    }
}
